package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadLocal.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 用户关注行为操作
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.参数校验
        if( dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"作者id为空");
        }

        //2.当前是否是用户登录还是游客
        ApUser apUser = ApUserThreadLocalUtil.getApUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        if(userId==0){
            //2.1 如果是游客，则不可以执行以下操作，终止
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"游客身份，没有权限关注用户");
        }

        //3.先判断当前用户是否关注过该作者
        String followKey="USER_FOLLOW_"+userId;
        String fansKey="USER_FANS_"+dto.getAuthorId();
        Boolean flag = redisTemplate.opsForHash().hasKey(followKey, dto.getAuthorId().toString());

        //4.如果关注过，并且此刻正在执行的是关注动作，则终止
        if(flag && dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前用户已经关注过该作者");
        }

        //5.如果关注过，但是次可以正在执行的是取消关注动作，则删除redis和mongo中的数据
        if(flag && dto.getOperation()==1){
            redisTemplate.opsForHash().delete(followKey,dto.getAuthorId().toString());
            redisTemplate.opsForHash().delete(fansKey,userId.toString());

            mongoTemplate.remove(Query.query(
                    Criteria.where("userId").is(userId).and("followId").is(dto.getAuthorId())
            ), ApFollowBehavior.class);
        }

        //6.如果没有关注过，此时正在行的是关注动作，则新增mongo和redis
        if(!flag && dto.getOperation()==0){
            redisTemplate.opsForHash().put(followKey,dto.getAuthorId().toString(),"1");
            redisTemplate.opsForHash().put(fansKey,userId.toString(),"1");

            ApFollowBehavior apFollowBehavior=new ApFollowBehavior();
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(Long.valueOf(userId));
            apFollowBehavior.setCreatedTime(new Date());
            apFollowBehavior.setArticleId(dto.getArticleId());
            mongoTemplate.save(apFollowBehavior);
        }

        //4.如果没有关注过，并且此刻正在执行的是取消关注动作，则终止
        if(!flag && dto.getOperation()==1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前用户取消了关注该作者");
        }

        //7.返回数据
        return ResponseResult.okResult("关注完成");
    }
}
